/**
 * 
 */
package com.ws.framework.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.ws.framework.dao.AppDao;
import com.ws.framework.model.App;
import com.ws.framework.model.Page;

/**
 * <p>
 * </p>
 *
 * @author WSH
 */
@Repository
public class AppDaoImpl implements AppDao {

	private static final String inserSql = "INSERT INTO t_app_info(app_code,app_name,managers,pub_key,pri_key) VALUES(?, ?, ?, ?, ?);";
	private static final String selectSql = "SELECT id, app_code AS appCode, app_name AS appName,managers AS codeManagers,create_time AS createTime,pub_key As pubKey,pri_key As priKey FROM t_app_info WHERE managers LIKE ?;";
	private static final String selectByIdSql = "SELECT id, app_code AS appCode, app_name AS appName,managers AS codeManagers,create_time AS createTime,pub_key As pubKey,pri_key As priKey FROM t_app_info WHERE id = ?;";
	private static final String selectByCodeSql = "SELECT id, app_code AS appCode, app_name AS appName,managers AS codeManagers,create_time AS createTime,pub_key As pubKey,pri_key As priKey FROM t_app_info WHERE app_code = ?;";
	private static final String queryForPageBaseSql = "SELECT id, app_code AS appCode, app_name AS appName,managers AS codeManagers,create_time AS createTime,pub_key As pubKey,pri_key As priKey FROM t_app_info WHERE 1 = 1";

	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Override
	public Page<App> select4Paget(Page<App> page) {
		String sql = queryForPageBaseSql + page.getAllLikeCondition();
		return page.get(sql, jdbcTemplate, App.class);
	}

	@Override
	public int insert(App app) {
		return jdbcTemplate.update(inserSql, app.getAppCode(), app.getAppName(), app.getCodeManagers(), app.getPubKey(), app.getPriKey());
	}

	@Override
	public List<App> selectAppByManager(String manager) {
		return jdbcTemplate.query(selectSql, new Object[]{"%" + manager + "%"}, new BeanPropertyRowMapper<App>(App.class));
	}

	@Override
	public App selectAppById(long id) {
		return jdbcTemplate.queryForObject(selectByIdSql, new Object[]{id}, new BeanPropertyRowMapper<App>(App.class));
	}

	@Override
	public App selectAppByCode(String code) {
		return jdbcTemplate.queryForObject(selectByCodeSql, new Object[]{code}, new BeanPropertyRowMapper<App>(App.class));
	}

}
